package com.jason.shader.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jason.shader.R;

import java.util.Arrays;

/**
 * desc:
 *
 *  Matrix类中会有三大类方法：setXXX、preXXX和postXXX，
 *  而preXXX和postXXX就是分别表示矩阵的左右乘，也有前后乘的说法
 *  注意：ColorMatrix和Matrix在应用给其他对象时都是左乘的，而其自身内部是可以左右乘的
 *
 *
 * user:jason
 * date:16-9-7
 * time:下午4:40
 */
public class MartixView6 extends View {
    private static final String TAG = MartixView6.class.getSimpleName();

    private static final int RECT_SIZE = 400;// 矩形尺寸的一半
    private Paint mPaint; // 画笔
    private int left, top, right, bottom; // 矩形左上右下坐标
    private int screenX, screenY;


    public MartixView6(Context context, AttributeSet attrs) {
        super(context, attrs);

        int widht = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        screenX = widht / 2;
        screenY = height / 2;

        left = screenX - RECT_SIZE;
        top = screenY - RECT_SIZE;
        right = screenX + RECT_SIZE;
        bottom = screenY + RECT_SIZE;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);

        BitmapShader bitmapShader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix = new Matrix();
        matrix.setTranslate(500, 500);
//        matrix.setRotate(5);  //旋转后 平移的效果没有了
        matrix.preRotate(5);
//        matrix.postRotate(5);

        float[] fs = new float[9];
        matrix.getValues(fs);
        Log.e(TAG, Arrays.toString(fs));
        // 设置矩阵变换
        bitmapShader.setLocalMatrix(matrix);
        mPaint.setShader(bitmapShader);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0, 0, screenX * 2, screenY * 2, mPaint);
    }
}
